﻿//© 2010-2011 mOcean Mobile. A subsidiary of Mojiva, Inc. All Rights Reserved.

using System;
using System.Drawing;
using System.Windows.Forms;
using mOceanWindows;

namespace SampleBrowser
{
    public partial class frmSampleBrowser : Form
    {
        private AdViewControl _ad1 = new AdViewControl();
        private AdViewControl _ad2 = new AdViewControl();
        private AdViewControl _ad3 = new AdViewControl();

        public frmSampleBrowser()
        {
            InitializeComponent();

            webBrowser.StatusBar = statusStrip;
            webBrowser.AddressBox = txtAddress;

            _ad1.Size = new Size(340,60);
            _ad2.Size = new Size(420, 260);
            _ad3.Size = new Size(420, 260);

            _ad1.AdNavigateBanner += new AdViewControl.AdNavigateBannerDelegate(_ad_AdNavigateBanner);
            _ad2.AdNavigateBanner += new AdViewControl.AdNavigateBannerDelegate(_ad_AdNavigateBanner);
            _ad3.AdNavigateBanner += new AdViewControl.AdNavigateBannerDelegate(_ad_AdNavigateBanner);

            toolStrip3.Items.Add(new ToolStripControlHost(_ad1));
            toolStrip4.Items.Insert( 2, new ToolStripControlHost(_ad2));
            toolStrip4.Items.Add(new ToolStripControlHost(_ad3));


            cmbIABSize.SelectedIndex = 0;
            cmbMMASize.SelectedIndex = 0;
            cmbRich.SelectedIndex = 0;

            _ad1.ZoneId = "16741";
            _ad1.SiteId = "6277";

            _ad1.ImageMaxWidth = 320;
            _ad1.ImageMaxHeight = 50;
            _ad1.ImageMinWidth = 320;
            _ad1.ImageMinHeight = 50;

            //_ad1.ViewId = 1;
            //__hook( &CAdViewControl::OnAdNavigateBanner, m_ad1, &CMainFrame::OnNavigateBanner );

            _ad1.InternalBrowser = true;
            _ad1.ReloadPeriod = 30000;
            _ad1.AppId = "abCdefG";

            _ad1.Run();

            _ad2.ZoneId = "16741";
            //_ad2.SiteId = "6277";
            _ad2.Mode = (int)AdViewControl.ADS_MODE.MODE_ADS_ONLY;
            _ad2.AdsType = (int)AdViewControl.ADS_TYPE.ADS_TYPE_IMAGES_ONLY;
            //_ad2.UseCache = TRUE;
            _ad2.ImageMaxWidth = 300;
            _ad2.ImageMaxHeight = 250;
            _ad2.ImageMinWidth = 300;
            _ad2.ImageMinHeight = 250;
            _ad2.ViewId = 2;

            _ad2.InternalBrowser = true;
            _ad2.ReloadPeriod = 30000;
            _ad2.AppId = "abCdefG";
            _ad2.Run();

            _ad3.ZoneId = "16801";
            _ad3.SiteId = "6277";
            _ad3.Mode = (int)AdViewControl.ADS_MODE.MODE_ADS_ONLY;
            _ad3.AdsType = (int)AdViewControl.ADS_TYPE.ADS_TYPE_TEXT_AND_IMAGES;
            //_ad3.UseCache = TRUE;
            _ad3.ViewId = 3;

            _ad3.InternalBrowser = true;
            _ad3.ReloadPeriod = 30000;
            _ad3.AppId = "abCdefG";
            _ad3.Run();

            statusStrip.Items.Add("");
            webBrowser.Navigate("http://www.moblogga.mobi/");
        }

        bool _ad_AdNavigateBanner(string url)
        {
            webBrowser.Navigate(url);
            return true;
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            webBrowser.GoBack();
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            webBrowser.Stop();
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            webBrowser.Refresh();
        }

        private void btnCut_Click(object sender, EventArgs e)
        {
            if (txtAddress.Focused) txtAddress.Cut();
            else if (webBrowser.Document != null) webBrowser.Document.ExecCommand("Cut", false, null);
        }

        private void btnCopy_Click(object sender, EventArgs e)
        {
            if (txtAddress.Focused) txtAddress.Paste();
            else if (webBrowser.Document != null) webBrowser.Document.ExecCommand("Copy", false, null);
        }

        private void btnPaste_Click(object sender, EventArgs e)
        {
            if (txtAddress.Focused) txtAddress.Paste();
            else if (webBrowser.Document != null) webBrowser.Document.ExecCommand("Paste", false, null);
        }

        private void undoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (txtAddress.Focused) txtAddress.Undo();
            else if (webBrowser.Document != null) webBrowser.Document.ExecCommand("Undo", false, null);
        }

        private void btnForward_Click(object sender, EventArgs e)
        {
            webBrowser.GoForward();
        }

        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (webBrowser.Document != null) webBrowser.Document.ExecCommand("SaveAs", false, null);
        }

        private void printToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (webBrowser.Document != null) webBrowser.Document.ExecCommand("Print", false, null);
        }

        private void printPreviewToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (webBrowser.Document != null) webBrowser.Document.ExecCommand("PrintPreview", false, null);
        }

        private void pageSetupToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (webBrowser.Document != null) webBrowser.Document.ExecCommand("PageSetup", false, null);
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void homeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            webBrowser.GoHome();
        }

        private void txtAddress_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Return)
            {
                webBrowser.Navigate(txtAddress.Text);
            }
        }

        private void toolbarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            toolbarToolStripMenuItem.Checked = !toolbarToolStripMenuItem.Checked;
            toolStrip1.Visible = toolbarToolStripMenuItem.Checked;
        }

        private void cmbMMASize_SelectedIndexChanged(object sender, EventArgs e)
        {
            int nSel = cmbMMASize.SelectedIndex;
			switch(nSel)
			{
			case 0:
				_ad1.ImageMinWidth = 320;
				_ad1.ImageMaxWidth = 320;
				_ad1.ImageMinHeight = 50;
				_ad1.ImageMaxHeight = 50;
				_ad1.Run();
				break;
			case 1:
				_ad1.ImageMinWidth = 300;
				_ad1.ImageMaxWidth = 300;
				_ad1.ImageMinHeight = 50;
				_ad1.ImageMaxHeight = 50;
				_ad1.Run();
				break;
			default:
				_ad1.ImageMinWidth = 216;
				_ad1.ImageMaxWidth = 216;
				_ad1.ImageMinHeight = 36;
				_ad1.ImageMaxHeight = 36;
				_ad1.Run();
				break;
			}

        }

        private void cmbIABSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            int nSel = cmbIABSize.SelectedIndex;
            switch (nSel)
            {
                case 0:
                    _ad2.ImageMinWidth = 300;
                    _ad2.ImageMaxWidth = 300;
                    _ad2.ImageMinHeight = 250;
                    _ad2.ImageMaxHeight = 250;
                    _ad2.Run();
                    break;
                case 1:
                    _ad2.ImageMinWidth = 468;
                    _ad2.ImageMaxWidth = 468;
                    _ad2.ImageMinHeight = 60;
                    _ad2.ImageMaxHeight = 60;
                    _ad2.Run();
                    break;
                case 2:
                    _ad2.ImageMinWidth = 728;
                    _ad2.ImageMaxWidth = 728;
                    _ad2.ImageMinHeight = 90;
                    _ad2.ImageMaxHeight = 90;
                    _ad2.Run();
                    break;
                case 3:
                    _ad2.ImageMinWidth = 160;
                    _ad2.ImageMaxWidth = 160;
                    _ad2.ImageMinHeight = 600;
                    _ad2.ImageMaxHeight = 600;
                    _ad2.Run();
                    break;
                case 4:
                    _ad2.ImageMinWidth = 600;
                    _ad2.ImageMaxWidth = 600;
                    _ad2.ImageMinHeight = 90;
                    _ad2.ImageMaxHeight = 90;
                    _ad2.Run();
                    break;
                case 5:
                    _ad2.ImageMinWidth = 120;
                    _ad2.ImageMaxWidth = 120;
                    _ad2.ImageMinHeight = 600;
                    _ad2.ImageMaxHeight = 600;
                    _ad2.Run();
                    break;
                default:
                    _ad2.ImageMinWidth = 300;
                    _ad2.ImageMaxWidth = 300;
                    _ad2.ImageMinHeight = 100;
                    _ad2.ImageMaxHeight = 100;
                    _ad2.Run();
                    break;
            }

        }

        private void cmbRich_SelectedIndexChanged(object sender, EventArgs e)
        {
            int nSel = cmbRich.SelectedIndex;
            switch (nSel)
            {
                case 0:
                    _ad3.ZoneId = "16801";
                    _ad3.Run();
                    break;
                case 1:
                    _ad3.ZoneId = "16803";
                    _ad3.Run();
                    break;
                default:
                    _ad3.ZoneId = "16804";
                    _ad3.Run();
                    break;
            }

        }

        private void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            txtAddress.Text = webBrowser.Url.ToString();
        }

        private void webBrowser_Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {

        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (frmAbout frm = new frmAbout())
            {
                frm.ShowDialog();
            }
        }
    }

    public class WebBrowserEx : WebBrowser
    {
        private ToolStripTextBox _tb;
        private StatusStrip _sb;
        public WebBrowserEx()
        {
        }

        public ToolStripTextBox AddressBox
        {
            set
            {
                _tb = value;
            }
        }

        public StatusStrip StatusBar
        {
            set
            {
                _sb = value;
            }
        }

        protected override void OnDocumentTitleChanged(EventArgs e)
        {
            base.OnDocumentTitleChanged(e);

            Parent.Parent.Parent.Text = DocumentTitle;
        }

        protected override void OnStatusTextChanged(EventArgs e)
        {
            base.OnStatusTextChanged(e);
            _sb.Items[0].Text = StatusText;
        }
    } ;
}
